package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.lang.reflect.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Modify a TAGs accounting
 * @author S.N.Fraser
 * $Id$
 */
public class EDIT_TAGImpl extends TransactionImpl {
    
    /** The Tag data to insert into db root.*/
    protected Tag tag;
    
    public EDIT_TAGImpl(Tag tag) {
	super();
	this.tag = tag;
    }
    
    public EDIT_TAGImpl(EDIT_TAG request) throws InvocationTargetException {
	super(request);
	this.tag = request.getTag();	
    }

    /**
     * Modfies the Tag in the DB.
     * @return result.
     */
    public TRANSACTION_DONE exec() { 

	Transaction t = null;
	try {
	    t = Transaction.current();
 
	    Root root = OSS_Utilities.fetchRoot();
	    
	    // Check if Root exists.
	    if (root == null) {
		logger.log(1, "EDITTAG_Impl", id, "exec",
			   "Root not found in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR,
				     "Could not find DB Root");
	    }

	    Path tagPath = new Path(tag.getFullPath());
	   
	    // the current TAG
	    Tag  ctag     = OSS_Utilities.fetchTag(tagPath);

            if (ctag == null)
                return error(TRANSACTION.PATH_RESOLUTION_ERROR,
                             "No such TAG ["+tagPath+"] found");

	    // copy all fields across...

	    ctag.setTotalAllocatedTime(tag.getTotalAllocatedTime());
	    ctag.setTotalAchievedTime(tag.getTotalAchievedTime());

	    ctag.setTargetFraction(Group.BRIGHT,Group.POOR,      tag.getTargetFraction(Group.BRIGHT,Group.POOR));
	    ctag.setTargetFraction(Group.BRIGHT,Group.AVERAGE,   tag.getTargetFraction(Group.BRIGHT,Group.AVERAGE));
	    ctag.setTargetFraction(Group.BRIGHT,Group.EXCELLENT, tag.getTargetFraction(Group.BRIGHT,Group.EXCELLENT));
	    ctag.setTargetFraction(Group.DARK,Group.POOR,        tag.getTargetFraction(Group.DARK,Group.POOR));
            ctag.setTargetFraction(Group.DARK,Group.AVERAGE,     tag.getTargetFraction(Group.DARK,Group.AVERAGE));
            ctag.setTargetFraction(Group.DARK,Group.EXCELLENT,   tag.getTargetFraction(Group.DARK,Group.EXCELLENT));

	    ctag.setAchievedTime(Group.BRIGHT,Group.POOR,      tag.getAchievedTime(Group.BRIGHT,Group.POOR));
            ctag.setAchievedTime(Group.BRIGHT,Group.AVERAGE,   tag.getAchievedTime(Group.BRIGHT,Group.AVERAGE));
            ctag.setAchievedTime(Group.BRIGHT,Group.EXCELLENT, tag.getAchievedTime(Group.BRIGHT,Group.EXCELLENT));
            ctag.setAchievedTime(Group.DARK,Group.POOR,        tag.getAchievedTime(Group.DARK,Group.POOR));
            ctag.setAchievedTime(Group.DARK,Group.AVERAGE,     tag.getAchievedTime(Group.DARK,Group.AVERAGE));
            ctag.setAchievedTime(Group.DARK,Group.EXCELLENT,   tag.getAchievedTime(Group.DARK,Group.EXCELLENT));
	    
	    EDIT_TAG_DONE done = new EDIT_TAG_DONE(id);
	    done.setSuccessful(true);
	    return done;

	} finally {
	    t.commit();
	}
	
    }
    
}

/** $Log$ */
